<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('日志列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form action="" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">客户端名称</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="powerName" placeholder="" type="text">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="query" lay-submit>
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button class="pear-btn pear-btn-md" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="table" lay-filter="table"></table>
    </div>
</div>

<script id="toolbar" type="text/html">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script id="bar" type="text/html">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="detail"></i>
        日志
    </button>
    {{#  if((d.status=='0' || d.status=='2' || d.status=='3')){ }}
    <button class="pear-btn pear-btn-primary pear-btn-sm" data-id="{{d.id}}" lay-event="startClient"><i class="layui-icon"></i>启动</button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"></i>
        编辑
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"></i>
        删除
    </button>
    {{#  } }}
    {{#  if((d.status=='1')){ }}
    <a class="pear-btn pear-btn-primary pear-btn-sm" data-id="{{d.id}}" lay-event="stopClient"><i class="layui-icon"></i>停止</a>
    {{#  } }}
</script>

</body>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'popup'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let MODULE_PATH = "/canalClient/";
        let cols = [
            [
                {type: 'checkbox'},
                {field: 'title', minWidth: 150, title: '标题'},
                {field: 'instanceName', minWidth: 100, title: 'instanceName'},
                {field: 'filter', minWidth: 180, title: '过滤规则'},
                {field: 'host', minWidth: 120, title: 'IP'},
                {field: 'port', minWidth: 80, title: 'PORT'},
                {field: 'clusterEnabled', minWidth: 80, title: '启用集群', templet: function (d) {
                        return d.clusterEnabled == '1' ? "启用" : "停用"
                    }},
                {field: 'zookeeperAddress', minWidth: 180, title: 'zk地址'},
                {field: 'status', title: '状态', minWidth: 80, templet: function (d) {
                    if (d.status == '1') {
                        return "已启动";
                    }else if (d.status == '2') {
                        return "启动中";
                    }else if (d.status == '0') {
                        return "已停止";
                    }else if (d.status == '3') {
                        return "启动异常";
                    }
                    }},
                {title: '操作', templet: '#bar', width: 250, align: 'center'}
            ]
        ]

        table.render({
            elem: '#table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#toolbar',
            text: {none: '暂无记录信息'}
        });


        table.on('tool(table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'startClient') {
                window.startClient(obj);
            }else if (obj.event === 'stopClient') {
                window.stopClient(obj)
            }else if (obj.event === 'detail') {
                window.detail(obj)
            }
        });

        table.on('toolbar(table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            }
        });

        form.on('submit(query)', function (data) {
            window.refresh(data.field);
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '550px'],
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['550px', '500px'],
                content: MODULE_PATH + 'edit?id=' + obj.data['id']
            });
        }


        window.remove = function (obj) {
            layer.confirm('确定要删除该客户端', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
            window.refresh();
        }

        window.detail = function (obj) {

        }

        window.startClient = function (obj) {
            layer.confirm('确定要启动该实例', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'startClient/' + obj.data['id'],
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'put',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg("正在启动....", {icon: 1, time: 1000});
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
            window.refresh();
        }

        window.stopClient = function (obj) {
            layer.confirm('确定要停止该实例', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'stopClient/' + obj.data['id'],
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'put',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg("正在停止。。。", {icon: 1, time: 1000});
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
            window.refresh(data.field);
        }


        window.refresh = function (param) {
            table.reload('table', {where: param});
        }
        })
</script>
</html>